﻿using System;
using System.Linq;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Time;

namespace Super_Management_Mayhem.Screens.Playing.GameMode
{
    public class MoneyMayhem : Container, IGameMode
    {
        private readonly Timer _gameTimer;
        private readonly IStateChanger _playscreen;

        public int GameTimeLimit
        {
            get
            {
                return 180000;
            }
        }

        public int[] GetScores()
        {
            return Playscreen.Map.Players.Select(x => x.GetPoints()).ToArray();
        }

        public bool HasScore
        {
            get
            {
                return true;
            }
        }

        public bool HasTimeLimit
        {
            get
            {
                return true;
            }
        }

        public bool IsStarted { get; private set; }

        public MoneyMayhem(IStateChanger playscreen)
        {
            _playscreen = playscreen;

            AddTimers(_gameTimer = new Timer(GameTimeLimit, false, EndGame));

            CallMethod(5000, Start);
        }

        public int GetRemainingTime()
        {
            return (int)Math.Ceiling((1 - _gameTimer.PercentageElapsed) * GameTimeLimit / 1000) * 1000;
        }

        public void Start()
        {
            IsStarted = true;
            _gameTimer.Start();

            _playscreen.StartGame();
        }

        private void EndGame()
        {
            IsStarted = false;

            _playscreen.EndGame();
        }
    }
}
